package com.zhku.meeting.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhku.meeting.entities.Msg;
import com.zhku.meeting.entities.Room;
import com.zhku.meeting.entities.MeetingStatus;
import com.zhku.meeting.mapper.MeetingRecordMapper;
import com.zhku.meeting.mapper.RoomMapper;
import com.zhku.meeting.mapper.ShowStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private ShowStatusMapper showStatusMapper;

    @Autowired
    private MeetingRecordMapper meetingRecordMapper;

    private Room room ;
    private List<Room> rooms;

    public Msg getRoom(Map<String,Object> para){
        Integer roomId = Integer.parseInt((String)para.get("roomId"));
        Room room = roomMapper.selectByPrimaryKey(roomId);
        Map<String ,Object> map = new HashMap<>();
        map.put("result",room);
        return room!=null?Msg.success().add(map):Msg.fault();
    }

    public Msg getAll(Map<String,Object> para) {
        Map<String ,Object> map = new HashMap<>();
        if(para.containsKey("currentPage")){
            Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
            Integer pageSize = Integer.parseInt((String)para.get("pageSize"));
            PageHelper.startPage(currentPage,pageSize);
            rooms = roomMapper.selectAll();
            PageInfo pageInfo = new PageInfo(rooms);
            map.put("result",pageInfo);
        }else{
            rooms = roomMapper.selectAll();
            map.put("result",rooms);
        }
        return Msg.success().add(map);
    }

    public Msg updateRoom(Map<String,Object> para) {
        Integer id = (Integer)para.get("roomId");
        String no=null;
        Integer size=null;
        Boolean status = null;
        Integer floor = null;
        String profile = null;
        Boolean desk = null;
        Boolean chair = null;
        Boolean main = null;
        Boolean monitor = null;
        Boolean camera = null;
        Boolean wireMicro = null;
        Boolean acousticEquip = null;
        Boolean strip2 = null;
        Boolean projector = null;
        Boolean curtain = null;
        Boolean wireEar = null;
        Boolean voiceBox = null;
        Boolean strip4 = null;
        if(!para.containsKey("roomStatus")){
            no = (String)para.get("roomNo");
            size = (Integer)para.get("roomSize");
            floor = (Integer) para.get("floor");
//            floor = Integer.parseInt((String) para.get("floor"));
            profile = (String) para.get("profile");
            desk  = (Boolean) para.get("desk");
            chair = (Boolean) para.get("chair");
            main = (Boolean) para.get("main");
            monitor = (Boolean) para.get("monitor");
            camera = (Boolean) para.get("camera");
            wireMicro = (Boolean) para.get("wireMicro");
            acousticEquip = (Boolean) para.get("acousticEquip");
            strip2 = (Boolean) para.get("strip2");
            projector = (Boolean) para.get("projector");
            curtain = (Boolean) para.get("curtain");
            wireEar = (Boolean) para.get("wireEar");
            voiceBox = (Boolean) para.get("voiceBox");
            strip4 = (Boolean) para.get("strip4");
        }else{
            status = (Boolean)para.get("roomStatus");
        }
        room = new Room(id,no,size,status,floor,profile,desk,chair,main,monitor,
                camera,wireMicro,acousticEquip,strip2,projector,curtain,wireEar,voiceBox,strip4
                );
        int num = roomMapper.updateByPrimaryKey(room);
        return num>0?Msg.success():Msg.fault();
    }

    public Msg addRoom(Map<String,Object> para) {
        String no = (String)para.get("roomNo");
        Integer size =(Integer)para.get("roomSize");
//        Integer floor = (Integer) para.get("floor");
        Integer floor = Integer.parseInt((String) para.get("floor"));
        String profile = (String) para.get("profile");
        room =new Room(null,no,size,null,floor,profile);
        //连同状态表也插入
        showStatusMapper.insert(room);
        int num = roomMapper.insert(room);
        return num>0?Msg.success():Msg.fault();
    }

    public Msg deleteRoom(Map<String,Object> para) {
        Integer id = (Integer)para.get("roomId");
        int num = roomMapper.deleteByPrimaryKey(id);
        showStatusMapper.deleteByPrimaryKey(id);
        meetingRecordMapper.deleteByCondition(id);
        return num>0?Msg.success():Msg.fault();
    }

    public Msg selectByCondition(Map<String, Object> para) {
//        Integer roomId = (Integer) para.get("roomId");
        //取出信息
        String no=null;
        Integer size=null;
        Boolean status=null;
        Integer floor = null;
        String profile = null;
        if (para.containsKey("roomNo")){
            no ="%"+para.get("roomNo")+"%";
        }
        if(para.containsKey("roomSize")){
            size = Integer.parseInt((String)para.get("roomSize"));
        }
        if(para.containsKey("roomStatus")){
            status = (Boolean)para.get("roomStatus");
        }
        if(para.containsKey("floor")){
            floor = (Integer) para.get("floor");
        }
        if(para.containsKey("profile")){
            profile = "%"+ para.get("profile")+"%";
        }
        Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
        Integer pageSize = Integer.parseInt((String)para.get("pageSize"));

        //封装
        Room room = new Room(null,no,size,status,floor,profile);

        //分页
        PageHelper.startPage(currentPage,pageSize);
        List<Room> rooms = roomMapper.selectByCondition(room);
        PageInfo pageInfo = new PageInfo(rooms);

        Map<String ,Object> map = new HashMap<>();
        map.put("result",pageInfo);
        return rooms.size()!=0?Msg.success().add(map):Msg.fault();
    }

    //查重
    public Msg duplicateCheck(Map<String, Object> para) {
        Integer roomId = (Integer) para.get("roomId") ;
        String roomNo = null;
        if(para.containsKey("roomNo")){
            roomNo = (String) para.get("roomNo");
        }
        String roomNo1  = roomMapper.selectRoomNoById(roomId);
        if(roomNo1.equals(roomNo)){
            return Msg.success();
        }else {
            Room room = new Room(null,roomNo,null,null,null,null);
            Room room1 = roomMapper.duplicateCheck(room);
            if(room1!=null){
                return Msg.fault().setError("该会议室号已经存在");
            }
            return  Msg.success();
        }
    }

    public Msg duplicateCheck1(Map<String, Object> para) {
        Integer roomId = (Integer) para.get("roomId") ;
        String roomNo = null;
        if(para.containsKey("roomNo")){
            roomNo = (String) para.get("roomNo");
        }
        String roomNo1  = roomMapper.selectRoomNoById(roomId);
        Room room = new Room(null,roomNo,null,null,null,null);
        Room room1 = roomMapper.duplicateCheck(room);
        if(room1!=null){
            return Msg.fault().setError("该会议室号已经存在");
        }
        return  Msg.success();
    }


    //根据占用的会议室id查询空闲的会议室
    public Msg getFree(Map<String, Object> para) {
        Map<String,Object> map = new HashMap<>();
        List<MeetingStatus> statuses;
        if(para.containsKey("currentPage")){
            Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
            Integer pageSize = Integer.parseInt((String)para.get("pageSize"));
            PageHelper.startPage(currentPage,pageSize);
            //查出所有
            statuses = showStatusMapper.selectAll();
            PageInfo pageInfo = new PageInfo(statuses);
            map.put("result",pageInfo);
        }else{
            statuses = showStatusMapper.selectAll();
            map.put("result",statuses);
        }
        return Msg.success().add(map);
    }

    public List<Room> getRooms() {
        return roomMapper.selectAll();
    }

//    public Msg updateEquipment(Map<String, Object> para) {
//        Integer id = (Integer)para.get("roomId");
//        Boolean air = null;
//        Boolean projector = null;
//        if(para.containsKey("air")){
//            air = (Boolean)para.get("air");
//            showStatusMapper.updateEquipment(id,air,projector);
//            roomMapper.updateEquipment(id,air,projector);
//            return Msg.success();
//        }
//        projector = (Boolean)para.get("projector");
//        showStatusMapper.updateEquipment(id,air,projector);
//        roomMapper.updateEquipment(id,air,projector);
//        return Msg.success();
//    }
}
